package com.cskaoyan.service.goods;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.goods.AdminCommentDeleteBO;
import com.cskaoyan.bean.bo.goods.AdminCommentListBO;
import com.cskaoyan.bean.po.goods.AdminCommentDeletePO;
import com.cskaoyan.bean.po.goods.AdminCommentListPO;
import com.cskaoyan.bean.po.goods.AdminGoodsListPO;
import com.cskaoyan.bean.vo.goods.AdminCommentListVO;
import com.cskaoyan.bean.vo.goods.AdminGoodsListVO;
import com.cskaoyan.mapper.goods.AdminCommentDeleteMapper;
import com.cskaoyan.mapper.goods.AdminCommentListMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/20 17:02
 */
@Data
@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    private Integer limit;

    private Integer page;

    private Integer pages;

    private Integer total;

    private List<AdminCommentListPO> list;

    @Autowired
    AdminCommentListMapper adminCommentListMapper;

    @Autowired
    AdminCommentDeleteMapper adminCommentDeleteMapper;



    @Override
    public AdminCommentListVO printCommentList(AdminCommentListBO adminCommentListBO) {

        page = adminCommentListBO.getPage();
        limit = adminCommentListBO.getLimit();

        Integer userId = adminCommentListBO.getUserId();
        Integer goodsId = adminCommentListBO.getValueId();
        String sort = adminCommentListBO.getSort();
        AdminCommentListVO adminCommentListVO = new AdminCommentListVO();

        if (limit != 0) {
            PageHelper.startPage(page, limit);
            QueryWrapper<AdminCommentListPO> adminCommentListPOQueryWrapper = new QueryWrapper<>();

            adminCommentListPOQueryWrapper.eq("deleted", 0);
            adminCommentListPOQueryWrapper.eq("type", 0);
            adminCommentListPOQueryWrapper.orderByDesc(sort);
            if (goodsId != null || userId != null) {

                adminCommentListPOQueryWrapper.eq(goodsId != null, "value_id", goodsId);
                adminCommentListPOQueryWrapper.eq(userId != null, "user_id", userId);
            }
            list = adminCommentListMapper.selectList(adminCommentListPOQueryWrapper);
            PageInfo<AdminCommentListPO> adminCommentListPOPageInfo = new PageInfo<>(list);
            total = (int) adminCommentListPOPageInfo.getTotal();
            pages = adminCommentListPOPageInfo.getPages();
            BeanUtil.copyBean(this, adminCommentListVO);
            return adminCommentListVO;
        }
        return adminCommentListVO;
    }


    @Override
    public void deleteComment(AdminCommentDeleteBO adminCommentDeleteBO) {
        Integer id = adminCommentDeleteBO.getId();
        Date date = new Date();

        AdminCommentDeletePO adminCommentDeletePO = new AdminCommentDeletePO();
        try {
            BeanUtils.copyProperties(adminCommentDeletePO, adminCommentDeleteBO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        adminCommentDeletePO.setUpdateTime(date);
        adminCommentDeletePO.setDeleted(true);
        QueryWrapper<AdminCommentDeletePO> adminCommentDeletePOQueryWrapper = new QueryWrapper<>();
        adminCommentDeletePOQueryWrapper.eq("id", id);
        adminCommentDeleteMapper.update(adminCommentDeletePO, adminCommentDeletePOQueryWrapper);
    }
}
